import Mock from 'mockjs';
import setupMock, { successResponseWrap, failResponseWrap } from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';
import { isLogin } from '@/utils/auth';

setupMock({
	setup() {
		// Mock.XHR.prototype.withCredentials = true;

		// 用户信息
		Mock.mock(new RegExp('/api/user/info'), () => {
			if (isLogin()) {
				const role = window.localStorage.getItem('userRole') || 'admin';
				return successResponseWrap({
					name: '王立群',
					avatar: '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
					email: 'wangliqun@email.com',
					job: 'frontend',
					jobName: '前端艺术家',
					organization: 'Frontend',
					organizationName: '前端',
					location: 'beijing',
					locationName: '北京',
					introduction: '人潇洒，性温存',
					personalWebsite: 'https://www.arco.design',
					phone: '150****0000',
					registrationDate: '2013-05-10 12:10:00',
					accountId: '15012312300',
					certification: 1,
					role
				});
			}
			return failResponseWrap(null, '未登录', 50008);
		});

		// 验证码
		Mock.mock(new RegExp('/v1/login/captcha'), () => {
			const data = {
				uuid: 'c43a493f-93eb-45ce-89aa-55fc4fdd5ccd',
				captcha:
					'',
				expire: 180
			};
			return successResponseWrap(data);
		});

		// 登录
		Mock.mock(new RegExp('/v1/login'), (params: MockParams) => {
			const { username, password } = JSON.parse(params.body);
			if (!username) {
				return failResponseWrap(null, '用户名不能为空', 50000);
			}
			if (!password) {
				return failResponseWrap(null, '密码不能为空', 50000);
			}
			return successResponseWrap({
				token: '12345'
			});
			// return failResponseWrap(null, '账号或者密码错误', 50000);
		});

		// 登出
		Mock.mock(new RegExp('/api/user/logout'), () => {
			return successResponseWrap(null);
		});

		// 用户的服务端菜单
		Mock.mock(new RegExp('/v1/menu/tree'), () => {
			const menuList = [
				{
					app: 'manager',
					title: '基础平台',
					icon: 'user',
					type: 'R',
					children: [
						{
							app: 'manager',
							path: '/manager/dashboard',
							keyword: 'managerDashboard',
							component: 'Layout',
							redirect: '/manager/dashboard/workplace',
							is_hidden: false,
							type: 'M',
							permission: '',
							icon: 'dashboard',
							order: 1,
							is_home: false,
							title: '基础平台',
							weight: 10,
							children: [
								{
									app: 'manager',
									path: '/manager/menu',
									keyword: 'managerMenu',
									component: 'manager/menu/index',
									redirect: '',
									is_hidden: false,
									type: 'M',
									permission: '',
									title: '菜单管理',
									icon: 'dashboard',
									weight: 10
								},
								{
									app: 'manager',
									path: '/manager/role',
									keyword: 'managerRole',
									component: 'manager/role/index',
									redirect: '',
									is_hidden: false,
									type: 'M',
									is_home: true,
									permission: '',
									title: '角色管理',
									icon: 'dashboard',
									weight: 10
								},
								{
									app: 'manager',
									path: '/manager/department',
									keyword: 'managerDepartment',
									component: 'manager/department/index',
									redirect: '',
									is_hidden: false,
									type: 'M',
									is_home: true,
									permission: '',
									title: '部门管理',
									icon: 'dashboard',
									weight: 10
								}
							]
						}
					]
				},
				{
					app: 'configure',
					title: '配置中心',
					icon: 'user',
					type: 'R',
					children: [
						{
							app: 'configure',
							path: '/configure/dashboard',
							keyword: 'configureDashboard',
							component: 'Layout',
							redirect: '/configure/dashboard/workplace',
							is_hidden: false,
							type: 'M',
							permission: '',
							icon: 'dashboard',
							order: 1,
							is_home: false,
							title: '配置管理',
							weight: 10,
							children: [
								{
									app: 'configure',
									path: '/configure/dashboard/workplace',
									keyword: 'configureWorkplace',
									component: 'manager/dashboard/workplace/index',
									redirect: '',
									is_hidden: false,
									type: 'M',
									permission: '',
									title: '配置中心1',
									icon: 'dashboard',
									weight: 10
								},
								{
									app: 'configure',
									path: '/configure/dashboard/workplace2',
									keyword: 'configureWorkplace2',
									component: 'manager/dashboard/workplace2/index',
									redirect: '',
									is_hidden: false,
									type: 'M',
									is_home: true,
									permission: '',
									title: '配置中心2',
									icon: 'dashboard',
									weight: 10
								}
							]
						}
					]
				}
			];
			return successResponseWrap(menuList);
		});
	}
});
